﻿package fwk.siteManager2.managers 
{
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.ui.Keyboard;
	

	public class CCodeManager 
	{
		private var _main:Object;
		private var _timer:Timer;
		private var _timerStarted:Boolean = false;
		private var _tabSequence:Array;
		
		// code up, up, down down, left, right, left, right, b, a
		private var _code1:Array = [Keyboard.UP, Keyboard.UP, Keyboard.DOWN, Keyboard.DOWN, Keyboard.LEFT, Keyboard.RIGHT, Keyboard.LEFT, Keyboard.RIGHT, 66, 65];
		private var _code1Checked:Boolean = false;
		
		// code d,e,b,u,g, enter
		private var _code2:Array = [68, 69, 66, 85, 71, Keyboard.ENTER];
		private var _code2Checked:Boolean = false;
		
		public function CCodeManager(main:Object) 
		{
			
			_main = main;
			
			_tabSequence = new Array();
			
			_timer = new Timer(3000, 1); // il faut taper le code en moins de 5 secondes
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
			
			_main.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownThis);
			
			
			
		}

		
		private function onKeyDownThis(e:KeyboardEvent):void 
		{
			if (!_timerStarted)
			{
				_timerStarted = true;
				_timer.start();
			}
			
			
			_tabSequence.push(e.keyCode);
			
			// check code 1
			if (!_code1Checked &&  _tabSequence.toString().indexOf( _code1.toString() ) != -1)
			{
				_code1Checked = true;
				onTimerComplete();
				_main.startCode1();
			}
			// check code 2
			if (!_code2Checked &&  _tabSequence.toString().indexOf( _code2.toString() ) != -1)
			{
				_code2Checked = true;
				onTimerComplete();
				_main.startCode2();
			}
			
		}
		private function onTimerComplete(e:TimerEvent = null):void 
		{
			
			_timerStarted = false;
			
			_tabSequence = null;
			_tabSequence = new Array();

		}
		

		
	}
	
}